//
//  TencentWeiboController.m
//  ShareDemo
//
//  Created by kai ren on 12/08/2011.
//  Copyright 2011 none. All rights reserved.
//

#import "TencentWeiboController.h"
#import "JSONKit.h"

@implementation TencentWeiboController

//Get catch path
- (NSString *)getCatchPath{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	return [NSString stringWithFormat:@"%@/%@", documentsDirectory, TENCENT_OAUTH_CACHE];
}

- (void)share{
    if ([OAuthInfo objectForKey:USER_NAME]) {
        NSMutableDictionary *files = [NSMutableDictionary dictionary];
        NSString *url = TencentShareUrl;
    
        QOauthKey *oauthKey = [[QOauthKey alloc] init];
        oauthKey.consumerKey = TencentAppKey;
        oauthKey.consumerSecret = TencentAppSecret;
        oauthKey.tokenKey = [OAuthInfo objectForKey:TOKEN_KEY];
        oauthKey.tokenSecret= [OAuthInfo objectForKey:TOKEN_SECRET];
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setObject:content forKey:@"content"];
        [parameters setObject:@"127.0.0.1" forKey:@"clientip"];
        [parameters setObject:@"json" forKey:@"format"];
    
        QWeiboRequest *request = [[QWeiboRequest alloc] init];
        [request asyncRequestWithUrl:url 
                          httpMethod:@"POST" 
                            oauthKey:oauthKey 
                          parameters:parameters 
                               files:files 
                            delegate:self];
    
        [request release];
        [oauthKey release];

    } 
}

- (void)getNick{
	if ([OAuthInfo objectForKey:USER_NAME]) {
        NSMutableDictionary *files = [NSMutableDictionary dictionary];
        NSString *url = TencentInfoUrl;
		
        QOauthKey *oauthKey = [[QOauthKey alloc] init];
        oauthKey.consumerKey = TencentAppKey;
        oauthKey.consumerSecret = TencentAppSecret;
        oauthKey.tokenKey = [OAuthInfo objectForKey:TOKEN_KEY];
        oauthKey.tokenSecret= [OAuthInfo objectForKey:TOKEN_SECRET];
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setObject:@"127.0.0.1" forKey:@"clientip"];
        [parameters setObject:@"json" forKey:@"format"];
		
        QWeiboRequest *request = [[QWeiboRequest alloc] init];
        [request asyncRequestWithUrl:url 
                          httpMethod:@"GET" 
                            oauthKey:oauthKey 
                          parameters:parameters 
                               files:files 
                            delegate:self];
		
        [request release];
        [oauthKey release];
    } 	
}

#pragma mark -
#pragma mark NSURLConnection Delegate

/** Delegate: Call when finish loading **/
- (void)connectionDidFinishLoading:(NSURLConnection *)conn{
	NSDictionary *result = [_data objectFromJSONData];
	NSString *resultContent = [[result objectForKey:@"data"] objectForKey:USER_NICK];
	WeiboOperationType operation;
	if (resultContent == nil ) {
		operation = WeiboOperationShare;
	} else {
		operation = WeiboOperationNick;
	}
		
 	if ([delegate respondsToSelector:@selector(connectionCompleteWithWeibo:operation:content:)]) {
		[delegate connectionCompleteWithWeibo:WeiboTencnet 
									operation:operation 
									  content:resultContent]; 
	}
}

@end
